package com.example.demo.controller;


import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Constants;
import com.example.demo.common.Result;
import com.example.demo.controller.dto.UserDTO;
import com.example.demo.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.demo.service.IUserService;
import com.example.demo.entity.User;

import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 残月
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/user")
        public class UserController {
    
@Resource
private IUserService userService;

@PostMapping("/login")
public Result login(@RequestBody UserDTO userDTO) {
String username = userDTO.getUsername();
String password = userDTO.getPassword();
if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
        return Result.error(Constants.CODE_400,"参数错误");
}
UserDTO dto = userService.login(userDTO);
 return Result.success(dto);
}

@PostMapping
public Result save(@RequestBody User user) {

        return Result.success(userService.saveOrUpdate(user));
        }

@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
        }

@PostMapping("/del/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(userService.removeByIds(ids));
        }

@GetMapping
public Result findAll() {
        return Result.success(userService.list());
        }

@GetMapping("username/{username}")
public Result findOne(@PathVariable String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return Result.success(userService.getOne(queryWrapper));
        }

        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(userService.getById(id));
        }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String username,
                                    @RequestParam(defaultValue = "") String email,
                                    @RequestParam(defaultValue = "") String body) {

                QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("id");
                if (!"".equals(username)) {
                        queryWrapper.like("username", username);
                }
                if (!"".equals(email)) {
                        queryWrapper.like("email", email);
                }
                if (!"".equals(body)) {
                        queryWrapper.like("body", body);
                }

                User currentUser = TokenUtils.getCurrentUser();
                System.out.println("当前用户==========" + currentUser.getNickname());

                return Result.success(userService.page(new Page<>(pageNum,pageSize), queryWrapper));

        }


        }

